package net.dgg.zz.credential.certificateWarning.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.StringUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.credential.certificateWarning.dto.CertificateWarningDto;
import net.dgg.zz.credential.certificateWarning.service.CertificateWarningService;
import net.dgg.zz.credential.certificateWarning.vo.CertificateWarningVo;
import net.dgg.zz.credential.certificateWarning.vo.TalentContractWarningVo;
import net.dgg.zz.credential.generalCredential.entity.GeneralCredentialVo;
import net.dgg.zz.credential.warningConfigure.constant.WarningType;
import net.dgg.zz.credential.warningConfigure.service.SystemConfigureService;
import net.fblock.log.NLog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author zsx
 * @Title: TalentContractExpireWarning
 * @ProjectName zzsc
 * @Description: 人才合同到期预警
 * @date 2018/8/22 24:19
 */
@Controller
@RequestMapping("/zs/talentContract/warning")
public class TalentContractExpireController extends DggBaseController {
    @Autowired
    private SystemConfigureService configureService;
    @Autowired
    private CertificateWarningService warningService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CommonService commonService;
    @Autowired
    private TranslateUtil translateUtil;
    @Autowired
    private DesUtil desUtil;


    /*
     * @Author zsx
     * @Date 2018-08-24 04:08:01
     * @Description   初始化人才合同预警
     */
    @AuthOpt(code = "ZS_TALENTCONTRACT_WARNING")
    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("GENERAL_CREDENTIAL", CredentialConstant.GENERAL_CREDENTIAL);
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("signOrgId", commonService.findUserSyb(sessionManager.getCurrentUser()));
            map.put("warningType", CredentialConstant.GENERAL_CREDENTIAL);
            map.put("warningName", WarningType.TALENT_CONTRACT_WARNING);
            Map<String, Object> warningMap = configureService.querySingleWarning(map);
            model.addAttribute("expireWarning", warningMap.get("configValue") == null ? 0 : warningMap.get("configValue"));
        } catch (Exception e) {
            model.addAttribute("expireWarning", "");
        }
        return "zzsc/certificateWarning/talent_contract_index";
    }


    /*
     * @Author zsx
     * @Date 2018-08-24 04:08:18
     * @Description 分页查询任何合同到期预警
     * @return DataTableResponse
     */
    @RequestMapping("/query_talent_contract")
    @ResponseBody
    @NLog()
    public DataTableResponse queryTalentContract(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        try {
            DataTableRequest dt = DataTableUtils.getParam(request);
            params.put("page", dt.getPage());
            params.put("limit", dt.getLength());
            if(params.containsKey("personalPhone") && !StringUtils.isEmpty(params.get("personalPhone"))) {
                params.put("personalPhone", desUtil.encrypt(params.get("personalPhone").toString()));
            }
            List<CertificateWarningDto> list = warningService.queryTalentContractWarningByWithPage(params);
            String[] column = {"certificateLevel"};
            list = translateUtil.translateList(column, list);
            String[] hideColum = {"personalPhone"};//隐藏
            list = desUtil.decryptList(hideColum,list);
            list = CustomerNumberEncryptionUtil.encryptionList(hideColum, list);
            return new DataTableResponse().data(params, list);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new DataTableResponse().data(params, null);
        }
    }

    /*
     * @Author zihao.wang
     * @Date 2020-07-13 13:12:20
     * @Description 导出excel
     * @param params
     * @return
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    @NLog()
    public void export(@RequestParam Map params, HttpServletResponse response) {
        try {
            if(params.containsKey("personalPhone") && !StringUtils.isEmpty(params.get("personalPhone"))) {
                params.put("personalPhone", desUtil.encrypt(params.get("personalPhone").toString()));
            }
            List<CertificateWarningDto> list = warningService.queryTalentContractWarningByWithPage(params);
            String[] column = {"certificateLevel"};
            list = translateUtil.translateList(column, list);
            String[] hideColum = {"personalPhone", "personalIdCard"};//隐藏
            list = desUtil.decryptList(hideColum,list);
            list = CustomerNumberEncryptionUtil.encryptionList(hideColum, list);
            List<TalentContractWarningVo> voList = new ArrayList<>();
            list.forEach(dto -> {
                TalentContractWarningVo vo = new TalentContractWarningVo();
                BeanUtils.copyProperties(dto, vo);
                if(vo.getTalentContractExpireTime() != null){
                    vo.setMessage("合同已到期" + DateUtils.transDate(vo.getTalentContractExpireTime(), new Date()));
                }
                voList.add(vo);
            });
            String fileName = "人才合同到期预警_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            ExcelUtils.writeToFileWithSuperClass(voList, null, fileName, response, TalentContractWarningVo.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

}
